package org.boun.swe.ARCEONE;

import java.io.IOException;

import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;

public class JsonClientActivity extends Activity {
	 private Socket client;
	 private PrintWriter printwriter;
	 private EditText textField;
	 private Button button;
	 private String messsage;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		textField = (EditText)findViewById(R.id.editText1);
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener (new View.OnClickListener(){
			
			public void onClick(View v) {
				 
			    messsage = textField.getText().toString(); //get the text message on the text field
			    textField.setText("");      //Reset the text field to blank
			 
			    try {
			 
			     client = new Socket("10.0.2.2", 9090);  //connect to server
			     printwriter = new PrintWriter(client.getOutputStream(),true);
			     printwriter.write(messsage);  //write the message to output stream
			 
			     printwriter.flush();
			     printwriter.close();
			     client.close();   //closing the connection
			 
			    } catch (UnknownHostException e) {
			     e.printStackTrace();
			    } catch (IOException e) {
			     e.printStackTrace();
			    }
			   }
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}